package com.andy.microservice.service2.controller;

import com.andy.microservice.service2.entity.Record;
import com.andy.microservice.service2.service.ProviderService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/provider")
public class ProviderController {

    @Autowired
    private ProviderService providerService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    MongoTemplate mongoTemplate;

    @GetMapping("/service2")
    public String providerService() throws InterruptedException {
        providerService.service();
        SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");
        String now = sdf.format(new Date());
        String message = "provide at:" + now;
        //MQ 发送消息
        rabbitTemplate.convertAndSend("provider", message);
//        Thread.sleep(3000);
        Record record = new Record();
        record.setContent(message);

        //MQ 生产记录存到mongodb
        mongoTemplate.insert(record,"record");
        //取出mongodb记录
        List<Record> recordList = mongoTemplate.findAll(Record.class,"record");
        System.out.println("recordList = " + recordList);
        return " | Send to RabbitMQ at" + now;
    }


//    @RabbitListener(queues = "provider")
//    public void getRabbitMessage(String msg) {
//        System.out.println("msg = " + msg);
//
//    }
}
